package com.hongdee.crm.base.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.base.dao.ICarDao;
import com.hongdee.crm.base.entity.TCrmSurveryArea;
import com.hongdee.crm.base.service.ITCrmBaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/11.
 */
@Service
public class TCrmBassAreaServiceImpl implements ITCrmBaseAreaService {
    @Autowired
    private ICarDao iCarDao;
    private String tabName = "T_CRM_SURVERY_ITEM_AREA";
    private String tapName = "T_CRM_SURVERY_ITEM";
    @Autowired
    private IHibernateDao iHibernateDao;

    public void update(String id, String status) throws Exception {

        String hql = "update TCrmSurveryArea set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.iCarDao.update(hql, params);
    }


    public TCrmSurveryArea saveOrUpdate(String uid,TCrmSurveryArea tCrmSurveryArea) throws Exception {
        tCrmSurveryArea.setCompanyId(TokenUtil.getCompanyId(uid));
        //根据区间编号、区间名称去重
        if (null == tCrmSurveryArea.getId() || StringUtils.isEmpty(tCrmSurveryArea.getId())) {
            //新增时判断是否已经存在
            if (isExist(tCrmSurveryArea.getAreaCode(), tCrmSurveryArea.getAreaName(), tCrmSurveryArea.getItemCode(), 0, null)) {
                throw new Exception("区间编号或区间名称名称已存在");
            }
            tCrmSurveryArea.setStatus("1");
        } else {
            //更新时的逻辑  更新时比较是不是跟以前存在的数据重复
            if (isExist(tCrmSurveryArea.getAreaCode(), tCrmSurveryArea.getAreaName(), tCrmSurveryArea.getItemCode(), 1, tCrmSurveryArea.getId())) {
                throw new Exception("区间编号或区间名称已存在");
            }
           // this.iCarDao.saveOrUpdate(tCrmSurveryArea);
        }
        // 判断单选项
        //根据问题项的id去查问题项纪录纪录数据
        Map map = this.iCarDao.info("t_crm_survery_item", "item_code", tCrmSurveryArea.getItemCode());
        if(map.size()>0){
            //判断我的问题项的类型是否为单项
            if (map.get("itemType").equals("dx") || map.get("itemType").equals("wb")) { // dx ： 类型代码，dx:单选 wb:文本
                //当我的问题项的类型为单项的时候，根据问题项的id去查询我的问题项对应评分项纪录,
                List params = this.iCarDao.list("select * from t_crm_survery_item_area where item_code = ?", tCrmSurveryArea.getItemCode());
                //判断评分编码非空 如果非空有数据返回异常提示
                if (null == params || params.size() == 0) {
                    this.iCarDao.saveOrUpdate(tCrmSurveryArea);
                } else {
                    throw new Exception("当前问题只允许存在一条区间");
                }
            }else{
                //当为多项的时候根据正常方式保存
                this.iCarDao.saveOrUpdate(tCrmSurveryArea);
            }
        }

        //修正下一级编码
        return tCrmSurveryArea;
    }
    //判断区间编号，区间名称是否重复
    private boolean isExist(String areaCode, String areaName, String itemCode, int type, String id) throws Exception {
        String sql = "select * from " + tabName + " where item_code = ? and(area_code= ? or area_name = ?)";
        List param = new ArrayList();
        param.add(itemCode);
        param.add(areaCode);
        param.add(areaName);
        //区间编号和区间名称不可以重复
        if (1 == type) {
            sql += " and  id != ? ";
            param.add(id);
        }
        Map map = this.iCarDao.page(sql, param.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;
    }

    /*
    * 根据条件查询
    * */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.iCarDao.info(sql, id);
    }

    /*
    * 分页
    * **/
    @Override
    public Map newPage(String start, String limit, String itemCode, String id) throws Exception {
        String sql = "select * from T_CRM_SURVERY_ITEM_AREA where 1=1 ";
          List<Object> params = new ArrayList();
         if(!StringUtils.isEmpty(itemCode)){
           sql += "and item_code = ? ";
            params.add(itemCode);
             }
          return this.iCarDao.page(sql, params.toArray(), start, limit);
    }

    public void impDel(String id) throws Exception {
        String hql = "delete from TCrmSurveryArea where id =:id ";
        Map params = new HashMap();
        params.put("id", id);
        this.iHibernateDao.executeHqlWithParams(hql, params);
    }
}
